package com.dgz.coolshark.controller;

import com.dgz.coolshark.mapper.ProductMapper;
import com.dgz.coolshark.pojo.dto.ProductUpdateDto;
import com.dgz.coolshark.pojo.vo.*;
import com.dgz.coolshark.pojo.dto.ProductDto;
import com.dgz.coolshark.pojo.entity.Product;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.sql.Timestamp;
import java.util.List;

/**
 * @Author duguozheng
 * @Date 2023/04/28  15:37
 * @Description
 * @Version 1.0
 */
@RestController
@RequestMapping("/product/")
public class ProductController {

    @Value("${imagesPath}")
    private String dirPath;

    @Autowired
    ProductMapper productMapper;

    @RequestMapping("insert")
    public void insert(@RequestBody ProductDto productDto) {

        /*System.out.println("productDto = " + productDto);//测试专用*/
        //把dto数据装进entity
        Product product = new Product();
        BeanUtils.copyProperties(productDto, product);
        product.setCreated(new Timestamp(System.currentTimeMillis()));
        product.setViewCount(0);
        productMapper.insert(product);
    }

    @RequestMapping("select")
    public List<ProductAdminVo> select() {
        return productMapper.selectForAdmin();
    }

    @RequestMapping("delete")
    public void deleteById(int id) {
        String url = productMapper.selectUrlById(id);
        new File(dirPath + url).delete();
        productMapper.deleteById(id);
    }

    @RequestMapping("selectById")
    public ProductUpdateVo selectById(int id) {
        return productMapper.selectById(id);
    }

    @RequestMapping("update")
    public void update(@RequestBody ProductUpdateDto productUpdateDto) {
        //得到原来的图片路径
        String url = productMapper.selectUrlById(productUpdateDto.getId());
        //判断新旧路径是否一致
        if (!url.equals(productUpdateDto.getUrl())) {
            new File(dirPath + url).delete();
        }
        Product product = new Product();
        BeanUtils.copyProperties(productUpdateDto, product);
        productMapper.update(product);
    }

    @RequestMapping("topList")
    public List<ProductTopListVo> topList() {
        return productMapper.selectTopList();
    }

    @RequestMapping("index")
    public List<ProductIndexVo> index() {
        return productMapper.selectIndex();
    }

    @RequestMapping("detail")
    public ProductDetailVo detail(int id, HttpSession session) {
        /**/
        String info = (String) session.getAttribute("view" + id);
        if (info == null) {//代表没有浏览过

            productMapper.updateViewCountById(id);
            session.setAttribute("view" + id, "visited");
        }
        return productMapper.selectDetailById(id);
    }

    @RequestMapping("category")
    public List<ProductIndexVo> category(int id) {
        return productMapper.selectByCategoryId(id);
    }

    @RequestMapping("search")
    public List<ProductIndexVo> search(String kwd) {
        return productMapper.selectByKwd(kwd);
    }
}
